from Cryptodome.Cipher import AES
from Cryptodome.Util.Padding import pad,unpad
import hashlib

# AES 加密函数
def aes_encode(data: bytes, key: str) -> bytes:
    # 将 key 处理成 16 字节
    key_bytes = hashlib.md5(key.encode()).digest()  # 16字节的key
    cipher = AES.new(key_bytes, AES.MODE_ECB)
    encrypted = cipher.encrypt(pad(data, AES.block_size))
    return encrypted
# AES 解密
def aes_decode(encrypted_data: bytes, key: str ) -> bytes:
    key_bytes = hashlib.md5(key.encode()).digest()
    cipher = AES.new(key_bytes, AES.MODE_ECB)
    decrypted = unpad(cipher.decrypt(encrypted_data), AES.block_size)
    return decrypted


# 示例用法
if __name__ == "__main__":
    key = "0XsW6Rn7"
    data = "秘密信息123ABC"  # 需要加密的字符串
    print("原始数据:", data)

    encrypted_data = aes_encode(data.encode(), key)
    print("加密后（十六进制）:", encrypted_data.hex())

    decrypted_data = aes_decode(encrypted_data, key)
    print("解密后:", decrypted_data.decode())